import pandas as pd

# 写入
def OutToFile(fileName, content):
    with open(fileName, "w", encoding="utf-8") as f:
        f.write(content)
    print("=== " + fileName + " end ===")

# 处理普通数据
def NormalToString(data):
    retStr = "[\n"
    for index, row in data.iterrows():
        retStr += "{\n"
        for col in data.columns:
            value = row[col]
            if pd.isna(value):
                value = ""
            if isinstance(value, str):
                retStr += f'"{col}": "{value}",\n'
            else:
                retStr += f'"{col}": {value},\n'
        retStr = retStr.rstrip(",\n") + "\n},\n"
    retStr = retStr.rstrip(",\n") + "\n]"
    return retStr

if __name__ == "__main__":
    # 读取CSV文件
    df = pd.read_csv("./data_config.csv")

    # 处理数据
    retStr = "{\n"
    retStr += '"LanguageConfig": ' + NormalToString(df) + "\n"
    retStr += "}"

    # 输出到JSON文件
    OutToFile("../assets/resources/config/cfg.json", retStr)
